type int = number;
//class PageTool

export class PageTool {
  constructor(arg1: Array<any>, arg2: int) {
    this._array = [];
    this.array = arg1;
    this._limit = arg2;
  }

  public getPrevPage(arg1: int): Array<any> {
    var loc1 = null;
    this._currentPage = arg1;
    if (this._currentPage <= 2) {
      loc1 = this.getByPageNo(1);
    } else if (this._currentPage <= this.pagesCount) {
      loc1 = this.getByPageNo(this._currentPage - 1);
    } else {
      loc1 = this.getByPageNo(this.pagesCount);
    }
    return loc1;
  }

  public getNextPage(arg1: int): Array<any> {
    var loc1 = null;
    this._currentPage = arg1;
    if (this._currentPage + 1 <= this.pagesCount) {
      loc1 = this.getByPageNo(this._currentPage + 1);
    } else {
      loc1 = this.getByPageNo(this.pagesCount);
    }
    return loc1;
  }

  public getByPageNo(arg1: int): Array<any> {
    var loc4 = 0;
    var loc1 = [];
    var loc2 = 0;
    var loc3 = 0;
    if (arg1 > 0) {
      loc2 = this.getStart(arg1);
      loc3 = this.getEnd(arg1);
      loc4 = loc2;
      while (loc4 <= loc3) {
        if (this.array[loc4] != undefined) {
          loc1.push(this.array[loc4]);
        }
        ++loc4;
      }
    }
    this.autoSetCurrentPage(arg1);
    return loc1;
  }

  public autoSetCurrentPage(arg1: int): void {
    if (arg1 <= 1) {
      this._currentPage = 1;
    } else if (arg1 > 1 && arg1 <= this.pagesCount) {
      this._currentPage = arg1;
    } else {
      this._currentPage = this.pagesCount;
    }
    return;
  }

  public getStart(arg1: int): int {
    var loc1 = 0;
    if (arg1 > 1) {
      loc1 = (arg1 - 1) * this.limit;
    }
    return loc1;
  }

  /* internal  */ getEnd(arg1: int): int {
    var loc1 = 0;
    if (arg1 != 1) {
      if (arg1 < this.pagesCount) {
        loc1 = this.getStart(arg1) + this.limit - 1;
      } else {
        loc1 = this.total - 1;
      }
    } else if (this._total < this.limit) {
      loc1 = this.total - 1;
    } else {
      loc1 = this.limit - 1;
    }
    return loc1;
  }

  public get array(): Array<any> {
    return this._array;
  }

  public set array(arg1: Array<any>) {
    if (arg1) {
      this._array = arg1;
      this._total = this._array.length;
    }
    return;
  }

  public get total(): number {
    return this._total;
  }

  public set total(arg1: number) {
    this._total = arg1;
    return;
  }

  public get pagesCount(): int {
    if (this.total == 0) {
      return 0;
    }
    if (this.total % this.limit == 0) {
      return this.total / this.limit;
    }
    return this.total / this.limit + 1;
  }

  public getPrevStartIndex(arg1: int): int {
    var loc1 = 0;
    this._currentPage = arg1;
    if (this._currentPage <= 2) {
      loc1 = this.getStart(1);
      this.autoSetCurrentPage(1);
    } else if (this._currentPage <= this.pagesCount) {
      loc1 = this.getStart(this._currentPage - 1);
      this.autoSetCurrentPage(this._currentPage - 1);
    } else {
      loc1 = this.getStart(this.pagesCount);
      this.autoSetCurrentPage(this.pagesCount);
    }
    return loc1;
  }

  public getNextStartIndex(arg1: int): int {
    var loc1 = 0;
    this._currentPage = arg1;
    if (this._currentPage + 1 <= this.pagesCount) {
      loc1 = this.getStart(this._currentPage + 1);
      this.autoSetCurrentPage(this._currentPage + 1);
    } else {
      loc1 = this.getStart(this.pagesCount);
      this.autoSetCurrentPage(this.pagesCount);
    }
    return loc1;
  }

  public set pagesCount(arg1: int) {
    this._pagesCount = arg1;
    return;
  }

  public get limit(): int {
    return this._limit;
  }

  public set limit(arg1: int) {
    this._limit = arg1;
    return;
  }

  public get currentPage(): int {
    return this._currentPage;
  }

  public set currentPage(arg1: int) {
    this._currentPage = arg1;
    return;
  }

  private /* var */ _array: Array<any>;

  private /* var */ _total!: int;

  private /* var */ _pagesCount!: int;

  private /* var */ _limit: int;

  private /* var */ _currentPage: int = 1;
}
